package com.yloveg.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.yloveg.entity.PaymentConfigure;
import com.yloveg.entity.ShopOrders;

public class PayUtil {
	public void goWebAlipay(ShopOrders order,PaymentConfigure payConfig,HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 商户订单号，商户网站订单系统中唯一订单号，必填
	    String out_trade_no = "PYA"+DateUtil.transPath();
		// 订单名称，必填
		String subject = "66666";

	    // 付款金额，必填
	    String total_amount=order.getOrderAmount().toString();
	    // 商品描述，可空
	    String body = "1212";
	    // 超时时间 可空
	    String timeout_express="2m";
	 
	    // 销售产品码 必填
	    String product_code="QUICK_WAP_PAY";
	    /**********************/
	    // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签     
	    //调用RSA签名方式
		AlipayClient client = new DefaultAlipayClient(payConfig.getGatewayUrl(),payConfig.getAppId(), payConfig.getMerchantPrivateKey(), payConfig.getPsaFormat(), payConfig.getCharset(), payConfig.getAlipayPublicKey(),payConfig.getSignType());
	    AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();
	    
	    // 封装请求支付信息
	    AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
	    model.setOutTradeNo(out_trade_no);
	    model.setSubject(subject);
	    model.setTotalAmount(total_amount);
	    model.setBody(body);
	    model.setTimeoutExpress(timeout_express);
	    model.setProductCode(product_code);
	    alipay_request.setBizModel(model);
	    alipay_request.setReturnUrl(payConfig.getReturnUrl());
	    alipay_request.setNotifyUrl(payConfig.getNotifyUrl());//在公共参数中设置回跳和通知地址 
	    
	    // form表单生产
	    String form = "";
		try {
			// 调用SDK生成表单
			form = client.pageExecute(alipay_request).getBody();
			response.setContentType("text/html;charset=" +payConfig.getCharset()); 
		    response.getWriter().write(form);//直接将完整的表单html输出到页面 
		    response.getWriter().flush(); 
		    response.getWriter().close();
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	public void goH5Alipay(ShopOrders order,PaymentConfigure payConfig,HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 商户订单号，商户网站订单系统中唯一订单号，必填
	    String out_trade_no = "PYA"+DateUtil.transPath();
		// 订单名称，必填
		String subject = "66666";

	    // 付款金额，必填
	    String total_amount=order.getOrderAmount().toString();
	    // 商品描述，可空
	    String body = "1212";
	    // 超时时间 可空
	    String timeout_express="2m";
	 
	    // 销售产品码 必填
	    String product_code="FAST_INSTANT_TRADE_PAY";
	    /**********************/
	    // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签     
	    //调用RSA签名方式
		AlipayClient client = new DefaultAlipayClient(payConfig.getGatewayUrl(),payConfig.getAppId(), payConfig.getMerchantPrivateKey(), payConfig.getPsaFormat(), payConfig.getCharset(), payConfig.getAlipayPublicKey(),payConfig.getSignType());
	    AlipayTradePagePayRequest alipay_request = new AlipayTradePagePayRequest();
	    // 封装请求支付信息
	    AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
	    model.setOutTradeNo(out_trade_no);
	    model.setSubject(subject);
	    model.setTotalAmount(total_amount);
	    model.setBody(body);
	    model.setTimeoutExpress(timeout_express);
	    model.setProductCode(product_code);
	    alipay_request.setBizModel(model);
	    alipay_request.setReturnUrl(payConfig.getReturnUrl());
	    alipay_request.setNotifyUrl(payConfig.getNotifyUrl());//在公共参数中设置回跳和通知地址 
	    
	    // form表单生产
	    String form = "";
		try {
			// 调用SDK生成表单
			form = client.pageExecute(alipay_request).getBody();
			response.setContentType("text/html;charset=" +payConfig.getCharset()); 
		    response.getWriter().write(form);//直接将完整的表单html输出到页面 
		    response.getWriter().flush(); 
		    response.getWriter().close();
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}


}
